package com.eat.fish_service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.eat.common_utils.R;
import com.eat.common_utils.ResultCode;
import com.eat.fish_service.entity.Dao.Disease;
import com.eat.fish_service.entity.Dao.Equipment;
import com.eat.fish_service.mapper.EquipmentMapper;
import com.eat.fish_service.netty_client.ManageConnections;
import com.eat.fish_service.netty_client.Message;
import com.eat.fish_service.service.EquipmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.netty.channel.Channel;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author CZF
 * @since 2022-04-11
 */
@Service
public class EquipmentServiceImpl extends ServiceImpl<EquipmentMapper, Equipment> implements EquipmentService {

    @Autowired
    private EquipmentService equipmentService;


    @Override
    public R operateEquipment(Long aquariumId, String type, byte behavior) {
        //获取要操作的鱼缸的netty通道
        Channel channel = ManageConnections.channelList.get(aquariumId);
        if(channel == null){
            return R.error().code(ResultCode.AQUARIUM_NETWORK_ERROR).message("鱼缸网络异常");
        }
        Message message = new Message();
        message.setHead(new byte[]{6, 6, 6, 6});
        message.setUpdate(new byte[]{0});
        switch (type){
            case "蜂鸣器":
                message.setFengMingQi(new byte[]{behavior});
                break;
            case "白灯":
            case "蓝灯":
            case "橙灯":
                message.setLight(new byte[]{behavior});
                break;
            case "打氧设备":
                message.setOxygen(new byte[]{behavior});
                break;
            case "投喂器":
                message.setFeed(new byte[]{behavior});
                break;
            case "紫外灯":
                message.setUv_lamp(new byte[behavior]);
                break;
            case "排水阀":
                message.setOutlet(new byte[]{behavior});
                break;
            case "进水阀":
                message.setInlet(new byte[]{behavior});
                break;
            default:
                return R.error().message("参数错误！");
        }

        channel.writeAndFlush(message);

        //TODO:更新equipment表的状态,后期可以在netty的入栈handler中添加
        Equipment equipment = new Equipment();
        equipment.setPid(aquariumId);
        equipment.setState((int)behavior);

        UpdateWrapper<Equipment> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("type",type);

        baseMapper.update(equipment,updateWrapper);


        return R.ok();
    }

    @Override
    public R queryEquipmentStatus(Long aquariumId) {


        List<Equipment> equipments = baseMapper.selectList(new QueryWrapper<Equipment>().eq("pid", aquariumId));

        if(equipments.size() == 0){
            return R.error().message("该鱼缸设备为空!");
        }

        return R.ok().data("equipments",equipments);
    }

    @Override
    public R getEquipment(String aquariumId) {

        //构建条件
        QueryWrapper<Equipment> wrapper = new QueryWrapper<>();
        wrapper.eq("pid", aquariumId);

        List<Equipment> equipmentList = equipmentService.list(wrapper);

        return R.ok().data("EquipmentList",equipmentList);

    }
}
